package com.automation.autocode.controller;

import io.swagger.annotations.ApiOperation;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.automation.autocode.mappers.UserMapper;
import com.automation.autocode.model.User;

/**
 * 首页实现接口
 * @author ren.bo2
 *
 */
//@CrossOrigin(origins = { "https://127.0.0.1:9000", "null" })
@RestController
@EnableTransactionManagement
public class IndexController {

	@Autowired
	private UserMapper userMapper;

	private static Logger logger = Logger.getLogger(IndexController.class);;

	/**
	 * 首页接口
	 * @return
	 */
	@ApiOperation(value = "IOS自动化首页", notes = "IOS自动化首页")
	@GetMapping("/index")
	public User index() {
		// 记录debug级别的信息  
		logger.debug("This is debug message.");
		// 记录info级别的信息  
		logger.info("This is info message.");
		// 记录error级别的信息  
		logger.error("This is error message.");
		User user = userMapper.findOne(1);
		return user;
	}

	/**
	 * 
	 * @param request
	 * @return
	 */
	@GetMapping(value = "/first")
	public Map<String, Object> firstResp(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<>();
		request.getSession().setAttribute("request Url", request.getRequestURL());
		map.put("request Url", request.getRequestURL());
		return map;
	}

	/**
	 * 
	 * @param request
	 * @return
	 */
	@GetMapping(value = "/sessions")
	public Object sessions(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<>();
		map.put("sessionId", request.getSession().getId());
		map.put("message", request.getSession().getAttribute("request Url"));
		return map;
	}
}
